package level_ajax_query.servlet;

import com.google.gson.Gson;
import level_ajax_query.dao.CityDao;
import level_ajax_query.entry.City;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json="{}";
        // 获取请求参数
        String proid = request.getParameter("proid");
        if (proid!=null && !"".equals(proid.trim())){
            int cityProid=Integer.valueOf(proid);
            System.out.println(cityProid);
            // 创建cityDao对象
            CityDao c=new CityDao();
            List<City> city = c.getCity(cityProid);
            // 将city对象转换为一个json对象
            if (city!=null){
                Gson object=new Gson();
                json=object.toJson(city);
                System.out.println(json);
            }
        }
        // 输出数据
        response.setContentType("appplication/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.println(json);
        writer.flush();
        writer.close();
    }
}
